package com.gollum.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.gollum.common.constant.SystemConstants;
import com.gollum.common.context.PageContextHolder;
import com.gollum.common.domain.response.TableSupport;
import com.gollum.common.utils.BeanUtils;
import com.gollum.common.utils.redis.RedisCache;
import com.gollum.jpa.domain.Page;
import com.gollum.jpa.domain.PageQueryRequest;
import com.gollum.system.domain.request.SysOnlineRequest;
import com.gollum.system.domain.response.LoginUser;
import com.gollum.system.domain.response.SysOnlineResponse;
import com.gollum.system.service.SysOnlineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


import java.util.Collection;

/**
 * SysOnlineService
 *
 * @author Herther
 * @createTime 2022年09月23日 22:26:00
 * @since 1.0.0
 */
@Service
@Slf4j
public class SysOnlineServiceImpl implements SysOnlineService {


    @Autowired
    private RedisCache redisCache;




    @Override
    public TableSupport<SysOnlineResponse> listPage(SysOnlineRequest sysOnlineRequest) {
        PageQueryRequest page = PageContextHolder.get();
        //new ArrayList<SysOnlineResponse>
        List<SysOnlineResponse> listData = CollUtil.toList();
        //查询
        Collection<String> keys = redisCache.keys(SystemConstants.LOGIN_TOKEN_KEY + SystemConstants.WILDCARD);
        keys.forEach(key->{
            LoginUser loginUser = redisCache.getCacheObject(key);
            if (Objects.nonNull(loginUser)){
                setResponse(loginUser,listData);
            }
        });
        TableSupport<SysOnlineResponse> objectTableSupport = new TableSupport<>();

        Page<SysOnlineResponse> objects = page.toPage();

        List<SysOnlineResponse> pageList = listData.stream()
                .skip(objects.getStart()).limit(objects.getLimit())
                .sorted(Comparator.comparing(SysOnlineResponse::getLoginTime).reversed())
                .collect(Collectors.toList());


        objectTableSupport.setPage(page.getPage());
        objectTableSupport.setList(pageList);
        objectTableSupport.setPageSize(page.getPageSize());
        objectTableSupport.setTotal(listData.size());


        return objectTableSupport;

    }



    @Override
    public void forceLogout(String[] sessionIds) {
        List<String> redisTokenKeys = getRedisTokenKeys(sessionIds);
        long l = redisCache.deleteObject(redisTokenKeys);
        log.info("强制退出登录，删除redis中对象数量为：{}" ,l);
    }

    private void setResponse(LoginUser loginUser, List<SysOnlineResponse> listData) {
        SysOnlineResponse sysOnlineResponse = new SysOnlineResponse();
        BeanUtils.copyBeanProp(loginUser, sysOnlineResponse);
        sysOnlineResponse.setSessionId(loginUser.getToken());
        sysOnlineResponse.setLoginTime(new Date(loginUser.getLoginTime()));
        listData.add(sysOnlineResponse);
    }


    /**
    * 组装redis Key
    * @author Herther
    * @Date 3:53 下午 2022/10/4
    * @parma* @param null:
    * @return
    * @version 1.0.0
    **/
    private List<String> getRedisTokenKeys(String[] tokens){
        List<String> redisKeies = new ArrayList<>();
        Arrays.asList(tokens).forEach(token->{
            redisKeies.add(SystemConstants.LOGIN_TOKEN_KEY + token);
        });
        return redisKeies;
    }

}
